import type { RouteRecordName } from 'vue-router';
import type { EnhancedRouteLocation } from '@/typings/router.d';

const useRouteStore = defineStore('routeStore', {
  state: () => ({
    cacheRoutes: <RouteRecordName[]>[]
  }),
  actions: {
    setCacheRoutes(route: EnhancedRouteLocation) {
      if (!route.name || this.cacheRoutes.includes(route.name)) {
        return;
      }
      if (route?.meta?.keepAlive) {
        this.cacheRoutes.push(route.name);
      }
    },
    removeCacheRoutes(name: string) {
      let index = this.cacheRoutes.indexOf(name);
      if (index != -1) {
        this.cacheRoutes.splice(index, 1);
      }
    }
  }
});

export default useRouteStore;
